<html>
<head><meta charset="utf-8"><title>type ascription *in patterns* · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html">type ascription *in patterns*</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213985025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/213985025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#213985025">(Oct 20 2020 at 22:00)</a>:</h4>
<p>It seems like most of the worries about type ascription in the other thread are about its use in expressions.</p>
<p>How might people feel about it on non-top-level bindings in <em>patterns</em>?</p>
<p>Something like this seems rather nice, to me:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">parse</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">y</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">    </span><span class="nb">Err</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>For now I'm explicitly talking about putting this only on bindings, not on arbitrary sub-patterns, though that of course could be done too.</p>
<p>(boats also made a comment in this direction in the other thread, that probably helped raise this in my head.)</p>



<a name="213991474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/213991474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#213991474">(Oct 20 2020 at 23:23)</a>:</h4>
<p>Ascriptions in patterns feel like a natural thing to have. On top of the added readability (c.f. IDEs) I talked about in the other thread, it would also make <code>let</code> bindings be less special <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="213998130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/213998130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#213998130">(Oct 21 2020 at 01:07)</a>:</h4>
<p>For back-compat I think let still needs to be more than just <code>let $pat</code>, since </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">Wrapping</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>: <span class="nc">Wrapping</span><span class="o">&lt;</span><span class="kt">u64</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Wrapping</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span><span class="w"></span>
</code></pre></div>

<p>works today.  Getting to "let is just a pattern" needs the full &lt;<a href="https://github.com/Centril/rfcs/blob/rfc/generalized-type-ascription/text/0000-generalized-type-ascription.md#type-ascription-in-patterns">https://github.com/Centril/rfcs/blob/rfc/generalized-type-ascription/text/0000-generalized-type-ascription.md#type-ascription-in-patterns</a>&gt;.  (Or I suppose removing the <code>$pat: $type</code> in an edition, which arguably could make things easier to parse without accidentally allowing <code>let x:u64:u64 = 4;</code>.)</p>



<a name="214004068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/214004068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#214004068">(Oct 21 2020 at 03:09)</a>:</h4>
<p>What? I thought this was already stable</p>



<a name="214004123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/214004123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#214004123">(Oct 21 2020 at 03:10)</a>:</h4>
<p>I don't think <code>let x:u64:u64 = 4;</code> should be prevented either, if it is a natural consequence of the grammar</p>



<a name="214004438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/214004438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#214004438">(Oct 21 2020 at 03:18)</a>:</h4>
<p>oops, I see you are only talking about ascriptions on binders, not patterns. I want type ascriptions everywhere they can reasonably be placed, notwithstanding issues in expression syntax (in particular because this is not something where alternate syntax to do the same thing exists). There are named args objections to type ascriptions in a struct or enum pattern, but this could be solved by preferring named args over type ascription, and requiring parentheses to get the latter. What are thoughts on using parentheses like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="n">x</span>: <span class="kt">u32</span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">bla</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{(</span><span class="n">x</span>: <span class="kt">u32</span><span class="p">)}</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="n">x</span>: <span class="mi">0</span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="214078306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/type%20ascription%20%2Ain%20patterns%2A/near/214078306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/type.20ascription.20*in.20patterns*.html#214078306">(Oct 21 2020 at 16:39)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> I think I favor it in bindings. The precedence there is indeed quite strong.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>